﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ParkingLot.Utility.StockHelp
{
    /// <summary>
    /// UDP收包工厂 ---设计模式之简单工厂模式
    /// </summary>
    public class UDPReceiveFactory
    {
       
        /// <summary>
        /// 创建广播对象
        /// </summary>
        /// <param name="localPort">源端口</param>
        /// <param name="receivePort">目的端口</param>
        /// <returns></returns>
        public UDPReceiveHelp CreatePublicReceive(int localPort,int receivePort)
        {
            UdpClient udpClient = new UdpClient(localPort);
            IPEndPoint receivEndPoint= new IPEndPoint(IPAddress.Any, receivePort);
            udpClient.EnableBroadcast = true;
            return  new UDPReceiveHelp(udpClient, receivEndPoint);
        }

        /// <summary>
        /// 创建组播对象
        /// </summary>
        /// <param name="localPort">本地端口号</param>
        /// <param name="receivEndPoint">目的网络端点</param>
        /// <param name="joinMulticastList">组播的组网</param>
        /// <returns></returns>
        public UDPReceiveHelp CreateGroupReceive(int localPort, IPEndPoint receivEndPoint, List<IPAddress> joinMulticastList)
        {
            UdpClient udpClient = new UdpClient(localPort);
            foreach (var item in joinMulticastList)
            {
                udpClient.JoinMulticastGroup(item);
            }
            udpClient.EnableBroadcast = true;
            return new UDPReceiveHelp(udpClient, receivEndPoint);
        }

        /// <summary>
        /// 创建组播对象
        /// </summary>
        /// <param name="localPort">本地端口号</param>
        /// <param name="receivEndPoint">目的网络端点</param>
        /// <param name="joinMulticastList">组播的组网</param>
        /// <returns></returns>
        public UDPReceiveHelp CreateUniReceive(IPEndPoint localEndPoint, IPEndPoint receivEndPoint)
        {
            UdpClient udpClient = new UdpClient(localEndPoint);
            return new UDPReceiveHelp(udpClient, receivEndPoint);
        }




    }
}
